package com.qzct.controller;

import com.mybatisflex.core.paginate.Page;
import com.qzct.entity.vo.GoVo;
import com.qzct.entity.vo.ResultVo;
import com.qzct.service.AttractionsService;
import com.qzct.service.CuisinesService;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.beans.factory.annotation.Autowired;
import com.qzct.entity.Go;
import com.qzct.service.GoService;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 *  控制层。
 *
 * @author 闽闽
 * @since 2025-02-18
 */
@RestController
@Api(tags = "勇敢冒险1")
@RequestMapping("/go")
public class GoController {

    @Autowired
    private GoService goService;
    @Autowired
    AttractionsService attractionsService;
    @Autowired
    CuisinesService cuisinesService;

    /**
     * 添加。
     *
     * @param go 
     * @return {@code true} 添加成功，{@code false} 添加失败
     */
    @PostMapping("save")
    @ApiOperation("保存")
    public boolean save(@RequestBody @ApiParam("") Go go) {
        return goService.save(go);
    }

    /**
     * 根据主键删除。
     *
     * @param id 主键
     * @return {@code true} 删除成功，{@code false} 删除失败
     */
    @DeleteMapping("remove/{id}")
    @ApiOperation("根据主键")
    public boolean remove(@PathVariable @ApiParam("主键") Serializable id) {
        return goService.removeById(id);
    }

    /**
     * 根据主键更新。
     *
     * @param go 
     * @return {@code true} 更新成功，{@code false} 更新失败
     */
    @PutMapping("update")
    @ApiOperation("根据主键更新")
    public boolean update(@RequestBody @ApiParam("主键") Go go) {
        return goService.updateById(go);
    }

    /**
     * 查询所有。
     *
     * @return 所有数据
     */
    @GetMapping("list")
    @ApiOperation("查询所有")
    public List<Go> list() {
        return goService.list();
    }

    /**
     * 根据主键获取详细信息。
     *
     * @param id 主键
     * @return 详情
     */
    @GetMapping("getInfo/{id}")
    @ApiOperation("根据主键获取")
    public Go getInfo(@PathVariable @ApiParam("主键") Serializable id) {
        return goService.getById(id);
    }

    /**
     * 分页查询。
     *
     * @param page 分页对象
     * @return 分页对象
     */
    @GetMapping("page")
    @ApiOperation("分页查询")
    public Page<Go> page(@ApiParam("分页信息") Page<Go> page) {
        return goService.page(page);
    }
//    根据用户id获取勇敢冒险
    @GetMapping("getGos/{id}")
    @ApiOperation("根据用户id获取")
    public ResultVo getGos(@PathVariable @ApiParam("主键") Serializable id) {
        ArrayList<GoVo> list = new ArrayList<>();
        ArrayList<Go> gos = goService.getByuId(id);
        for (int i = 0; i < gos.size(); i++) {
            GoVo goVo = new GoVo<>();
            goVo.setType(gos.get(i).getType());
            goVo.setId(gos.get(i).getId());
            if (gos.get(i).getType().equals("景点")){
                goVo.setGo(attractionsService.getById(gos.get(i).getTid()));
            }else{
                goVo.setGo(cuisinesService.getById(gos.get(i).getTid()));
            }
            list.add(goVo);
        }
        return ResultVo.success("成功",list);
    }

}
